package com.lyz.dataStructure.algorithm.zijietiaodong2020;/**
 * @Author:759057893@qq.com Lyz
 * @Date: ${Date} 19:31
 * @Description:
 **/


import java.util.Scanner;

/*
牛牛的闹钟
 */
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int hh = 0,mm =0;
        int[] arr = new int[N];
        for(int i = 0;i<N;i++){
            hh = sc.nextInt();
            mm = sc.nextInt();
            arr[i] = (hh*60)+mm;
        }
        int X = sc.nextInt();
        int A =sc.nextInt();
        int B =sc.nextInt();
        int isGo = (A*60)+B;
        int min = 0;
        for(int i =0;i<arr.length;i++){
            if(isGo-(X+arr[i])<0){
                continue;
            }else {
                if(isGo-(X+arr[i])<isGo-(X+arr[min])){
                    min = i;
                }
            }
        }
        System.out.println(arr[min]/60+" "+arr[min]%60);
    }
}
